package com.leyou.order.listen;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.leyou.common.constants.MQConstants;
import com.leyou.item.client.ItemClient;
import com.leyou.order.dto.OrderStatusEnum;
import com.leyou.order.entity.Order;
import com.leyou.order.entity.OrderDetail;
import com.leyou.order.mapper.OrderDetailMapper;
import com.leyou.order.mapper.OrderMapper;
import com.leyou.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单消息监听器
 * <p>
 * 说明：
 * 1. @RabbitListener指定目标方法来作为消费消息的方法，通过注解参数指定所监听的队列或者Binding
 *
 * @author MoCha
 * @date 2020/4/14
 */
@Slf4j
@Component
public class OrderListener {


    @Autowired
    private ItemClient itemClient;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderService orderService;


    //接受消息进行还库存业务
    @RabbitListener(queues = MQConstants.Queue.REPAY_STOCK_QUEUE)
    public void repayState(Long orderId, Message message, Channel channel) throws Exception{
        log.info("开始接受消息！");
        // 通知 MQ 消息已被接收,可以ACK(从队列中删除)了
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        System.out.println("接收消息开始还库");
        System.out.println(orderId);
        Order order = orderMapper.selectById(orderId);
        if (order.getStatus()==1){
            Map<Long, Integer> map = new HashMap<>();
            //根据orderID查询orderDetail
            OrderDetail orderDetailentity = new OrderDetail();
            orderDetailentity.setOrderId(orderId);
            QueryWrapper<OrderDetail> query = Wrappers.query(orderDetailentity);
            List<OrderDetail> orderDetails = orderDetailMapper.selectList(query);
            orderDetails.forEach(orderDetail -> {
                map.put(orderDetail.getSkuId(),orderDetail.getNum());
            });
            //调用微服务还库存
            itemClient.addStock(map);
            //更改订单状态
            Order orderEntity = new Order();
            orderEntity.setOrderId(orderId);
            orderEntity.setStatus(OrderStatusEnum.NOSTOCK.value());
            orderService.delPageOrder(orderId);
            orderMapper.updateById(orderEntity);
        }

    }

    //接受消息进行更改订单状态业务
    @RabbitListener(queues = MQConstants.Queue.CHANGE_ORDER_STATE_QUEUE)
    public void changeOrderState(Long orderId, Message message, Channel channel) throws Exception{
        log.info("开始接受更改订单状态！");
        // 通知 MQ 消息已被接收,可以ACK(从队列中删除)了
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        Order order = orderMapper.selectById(orderId);
        if (order.getStatus()==7){
            //更改订单状态
            Order entity = new Order();
            entity.setOrderId(orderId);
            entity.setStatus(OrderStatusEnum.FAILURE.value());
            orderMapper.updateById(entity);
            orderService.delPageOrder(orderId);
            log.info("更改订单状态成功");
        }
    }
}
